<?php
/**
 * Created by PhpStorm.
 * User: chenzhitao
 * Date: 2018/4/19
 * Time: 下午5:01
 */

namespace App\Busi\Modules\Resource;

use App\Busi\Traits\CompanyTrait;
use App\Busi\Traits\UserTrait;
use App\Exceptions\ErrorCode\UserError;
use App\Exceptions\UserException;
use App\Http\Requests\CompanyInfoRo;
use App\Busi\Modules\BaseBusi;

use App\Models\CategoryCompany;
use App\Models\Company;
use App\Util\StringUtil;
use Illuminate\Http\Request;
use App\Util\QiniuUtil;
use Illuminate\Support\Facades\DB;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
//use Qiniu\Storage;
use Storage;
class UploadCompanyInfoBusi extends BaseBusi
{
    use UserTrait,CompanyTrait;
    public function handle(CompanyInfoRo $ro){
        $uid = $ro->uid;
        $desc = $ro->desc;
        $advantage = $ro->advantage;
        $subcategory_ids = $ro->subcategory_ids;
        $subcategory_ids =  $subcategory_ids['subcategory_ids'];




        $logo = $ro->logo;
        $album = $ro->album;

        $album = $album['album'];
        //对图片做截取操作 below
        $img_host = config("admin.upload.host");
        $host_count = strlen($img_host);
        $logo = substr($logo,$host_count,strlen($logo)-$host_count);
        foreach ($album as &$item){
           $item = substr($item,$host_count,strlen($item)-$host_count);

        }
        //对图片做截取操作 above
        $pictures = json_encode($album);



        $contact_name = $ro->contact_name;
        $contact_number = $ro->contact_number;

        $user = $this->getUserByUid($uid);
        if(!$user){
            throw new UserException(UserError::USER_NOT_EXIST);
        }
        $raw_company = $this->getCompanyByUid($uid);


        if($raw_company){
            $update = [
                "desc"=>$desc,
                "advantage"=>$advantage,
                "logo"=>$logo,
                "pictures"=>$pictures,
                "contact_name"=>$contact_name,
                "contact_number"=>$contact_number
            ];
            Company::where("uid",$uid)->update($update);
            $this->updateSubCategory($raw_company,$subcategory_ids);
            $data = [
                "msg" =>"更新成功"
            ];
            return $data;
        }
        $company = new Company();
        $company->uid = $uid;
        $company->desc = $desc;
        $company->advantage = $advantage;


        $company->logo = $logo;
        $company->pictures = $pictures;
        $company->contact_name = $contact_name;
        $company->contact_number = $contact_number;

        $res = $company->save()?1:0;
        $raw_company = $this->getCompanyByUid($uid);
        $this->saveSubCategory($raw_company,$subcategory_ids);

        $data = [
            "msg" =>"上传成功"
        ];
        return $data;


    }

    function updateSubCategory($company,$category_ids){
        if($company){
            //先删除之前的关联关系,再新增新的关联
            $this->deletePivotOnCateAndCompany($company['id']);
            foreach ($category_ids as $category_id){
                $pivot = new CategoryCompany();
                $pivot->company_id = $company["id"];
                $pivot->category_id = $category_id;
                $pivot->save();
            }

        }

    }
    function deletePivotOnCateAndCompany($company_id){
        $where = [
            ["company_id","=",$company_id]
        ];
        $data = CategoryCompany::where($where)->delete();

    }
    function saveSubCategory($company,$category_ids){
        if($company){
            foreach ($category_ids as $category_id){
                $pivot = new CategoryCompany();
                $pivot->company_id = $company["id"];
                $pivot->category_id = $category_id;
                $pivot->save();
            }

        }

    }
    function uploadImg($file){

        $host = config("admin.upload.host");

        $name =  Storage::put("",$file);
        return $name;
    }


}